// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Model.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Visualization
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using Microsoft.Robotics.IO;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// Represents a UI model, i.e. the data structure that back a visualization. 
    /// This class supports databinding.
    /// </summary>
    public abstract class Model : INotifyPropertyChanged
    {
        /// <summary>
        /// All the properties of the model object. This is used to notify the bound view about which properties to update.
        /// </summary>
        private List<string> allProperties;  

        /// <summary>
        /// The data binding event we use to notify the UI that the model data is ready for display.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets or sets the number of samples per second, calculated as average over the entire duration
        /// </summary>
        public double SamplesPerSecond
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the count of messages received.
        /// </summary>
        public long MessageCount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the last message received by the model agent.
        /// </summary>
        public AgentMessage LastMessage
        {
            get;
            set;
        }

        /// <summary>
        /// Saves the last message to a file
        /// </summary>
        public void SaveLastMessage()
        {
            Serializer.Save(string.Format("{0}-{1}.xml", this.LastMessage.GetType().Name, this.LastMessage.SequenceId), this.LastMessage);
        }

        /// <summary>
        /// The set of properties that have not changed. If null, all properties are updated. Override to filter which properties to skip.
        /// </summary>
        /// <returns>The properties to *not* refresh</returns>
        public virtual IEnumerable<string> GetUnchangedProperties()
        {
            return null; 
        }

        /// <summary>
        /// Sends a notification to the UI that the model has changed and the UI should refresh
        /// </summary>
        public void NotifyPropertyChanged()
        {
            // send a notification to any the data bound controls about the change
            if (this.PropertyChanged != null)
            {
                IEnumerable<string> unchangedProperties = this.GetUnchangedProperties();
                if (unchangedProperties == null)
                {
                    // the null parameter indicates that all model properties have changed
                    this.PropertyChanged(this, new PropertyChangedEventArgs(null));
                }
                else
                {
                    if (this.allProperties == null)
                    {
                        this.allProperties = new List<string>();
                        this.allProperties.AddRange(this.GetType().GetProperties().Select(p => p.Name));
                        this.allProperties.AddRange(this.GetType().GetFields().Select(p => p.Name));
                    }

                    foreach (string propertyName in this.allProperties.Except(unchangedProperties))
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                    }
                }
            }
        }
    }
}
